﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Graphics;

namespace Jemgine.Engine.Scripts
{
    [Category("System"), Node]
    public class ChangeTexture : Data.ScriptNode, Data.TexturedObject
    {
        public String Texture { get; set; }


        public ChangeTexture()
        {
            AddInput("Fire", null);
            AddReference("Texturable", typeof(Data.TexturedObject));
            AddOutput("Relay", null);
        }

        public override void FireTerminal(int InputID, Data.ScriptPayload Payload)
        {
            var Target = ReferenceTerminals[0].GetTarget(Payload) as Data.TexturedObject;
            if (Target != null) Target.SetTexture(Texture, Sim.EpisodeContent.GetTexture(Texture));
            OutputTerminals[0].Fire(Payload);
        }

        public void SetTexture(String ID, Microsoft.Xna.Framework.Graphics.Texture2D Texture)
        {
            this.Texture = ID;
        }
    }

    [Category("System"), Node]
    public class ChangeTextureWithinEntity : Data.ScriptNode
    {
        public String Texture { get; set; }
        public String ChangeTo { get; set; }

        public ChangeTextureWithinEntity()
        {
            AddInput("Fire", null);
            AddReference("Entity", typeof(Data.EntityRecord));
            AddOutput("Relay", null);
        }

        public override void FireTerminal(int InputID, Data.ScriptPayload Payload)
        {
            var NewTex = Sim.EpisodeContent.GetTexture(ChangeTo);
            if (NewTex == null) return;

            var Target = ReferenceTerminals[0].GetTarget(Payload) as Data.EntityRecord;
            if (Target != null)
                foreach (var Element in Target.AttachedElements)
                    if (Element is Data.TexturedObject && (Element as Data.TexturedObject).Texture == Texture)
                        (Element as Data.TexturedObject).SetTexture(ChangeTo, NewTex);

            OutputTerminals[0].Fire(Payload);
        }
    }

   
}
